﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;


namespace Besture.UI.Admin.Content
{
    public partial class ImgsSet : BasePage
    {
        //类型序号
        public string _TypeID = "0";
        //栏目编号 (添加的时候初始化所选栏目)
        public string PartID = StringHelper.Query("partid");

        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {

            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
            //初始化发布时间
            ReleaseTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            //不需要显示“顶级类别”
            ControlPart.NeedTopClass = false;

            ControlPart.CurrentPartID = StringHelper.Query("partid");
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id <= 0)
            {
                if (string.IsNullOrEmpty(PartID))
                {
                    PartID = "0";
                }
                //初始化栏目编号
                ControlPart.ParentId = Convert.ToInt32(PartID);
                _TypeID =
                    QueryDac.GetTable(
                        string.Format(
                            @"SELECT t2.[Value] FROM TBPart t LEFT JOIN TBDictionary t2 ON T.TypeID = T2.ID WHERE t.ID = {0}",
                            PartID)).Rows[0][0].ToString();
                //初始显示
                cbIsDisplay.Checked = true;
                return;
            }
            PartContentDac dac = new PartContentDac();
            PartContentModel model = dac.GetModel(id);

            Name.Text = model.Name;
            SubName.Text = model.SubName;
            ControlPart.ParentId = model.PartID;
            ControlImage.ImageUrl = model.ImageUrl;
            Source.Text = model.Source;
            Author.Text = model.Author;
            ReleaseTime.Text = DateTimeHelper.FormatStandard(model.ReleaseTime);
            Preface.Text = model.Preface;
            Detail.Text = model.Detail;
            Hits.Text = model.Hits.ToString();
            cbIsDisplay.Checked = Convert.ToBoolean(model.IsDisplay);
            cbWebStick.Checked = Convert.ToBoolean(model.WebStick);
            cbClassStick.Checked = Convert.ToBoolean(model.ClassStick);
            txtLinkUrl.Text = model.LinkUrl;

            UploadFile.FileUrl = model.Author;

            _TypeID = (Convert.ToInt32(model.TypeID) - 1).ToString();

        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            PartContentDac dac = new PartContentDac();
            PartContentModel model = null;
            BaseDac<PartContentModel> baseDac = new BaseDac<PartContentModel>();
            //添加
            if (!EditPage)
            {
                model = new PartContentModel();
                model.Name = Name.Text;
                model.SubName = SubName.Text;
                model.PartID = ControlPart.ParentId;
                model.ImageUrl = ControlImage.ImageUrl;
                model.Source = Source.Text;
                model.Author = Author.Text;
                model.ReleaseTime = DateTime.Parse(ReleaseTime.Text);
                model.Preface = Preface.Text;
                model.Detail = Detail.Text;
                model.Hits = Convert.ToInt32(Hits.Text);

                model.WebStick = Convert.ToInt32(cbWebStick.Checked);
                model.ClassStick = Convert.ToInt32(cbClassStick.Checked);
                model.IsDisplay = Convert.ToInt32(cbIsDisplay.Checked);

                model.LinkUrl = txtLinkUrl.Text;

                //类型
                model.TypeID = Convert.ToInt32(Request.Form["rdlType"]);
                //加载SEO基本数据
                baseDac.LoadSeo(model, ControlSeo.SeoTitle, ControlSeo.SeoKeywords, ControlSeo.SeoDescription);
                //加载添加基本数据
                baseDac.LoadAddBaseModel(model);
                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(ID);
                model.Name = Name.Text;
                model.SubName = SubName.Text;
                model.PartID = ControlPart.ParentId;
                model.ImageUrl = ControlImage.ImageUrl;
                model.Source = Source.Text;
                model.Author = Author.Text;
                model.Author = UploadFile.FileUrl;

                model.ReleaseTime = DateTime.Parse(ReleaseTime.Text);
                model.Preface = Preface.Text;
                model.Detail = Detail.Text;
                model.Hits = Convert.ToInt32(Hits.Text);
                model.WebStick = Convert.ToInt32(cbWebStick.Checked);
                model.ClassStick = Convert.ToInt32(cbClassStick.Checked);
                model.IsDisplay = Convert.ToInt32(cbIsDisplay.Checked);

                model.LinkUrl = txtLinkUrl.Text;
                model.TypeID = Convert.ToInt32(Request.Form["rdlType"]);



                //加载SEO基本数据
                baseDac.LoadSeo(model, ControlSeo.SeoTitle, ControlSeo.SeoKeywords, ControlSeo.SeoDescription);
                //加载修改基本数据
                baseDac.LoadUpdateBaseModel(model);

                dac.Update(model);
            }
            FunHelper.Alert("保存成功", ListUrl() + "?partid=" + PartID);

            
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl(ListUrl() + "?partid=" + PartID);
        }

        #endregion
    }
}